<?php
namespace App\Http\Controllers\Merchant;
use App\Http\Controllers\BaseController;
use App\Models\AdvSignUp;
use App\Models\Merchant;
use App\Models\ProducerAdv;
use Illuminate\Http\Request;

class ProducerAdsController extends BaseController
{

    public function index(Request $request) {
        $input = $request->input();
        $this->validate($request, [
            'type' => 'filled|in:my,all',
        ]);
        $type = $input['type'] ?? 'all';
        $fac_title= $input['fac_title'] ?? '';
        $search = ['start_at' => '', 'end_at' => '', 'order_sn' => '', 'title' => ''];
        $list = ProducerAdv::apply($search, $input)->when($type, function ($q) use($type) {
            if ($type == 'my') {
                $q->where('merchant_id', $this->getUserId());
            }
        })->when($fac_title, function ($q) use($fac_title) {
            $q->whereHas('merchant', function ($qt) use($fac_title){
                $qt->where('fac_title', 'like', '%' . $fac_title . '%');
            });
        })->withCertain('merchant', ['fac_title'])->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }

    public function store(Request $request) {
        $input = $request->input();
        $this->validate($request, [
            'title'  => 'required',
        ]);
        $input['merchant_id'] = $this->getUserId();
        $id = $request->input('id');
        if ($id) {
            ProducerAdv::where('id', $id)->update($input);
        } else {
            $input['adv_sn'] = create_order_no('a');
            $model = ProducerAdv::create($input);
            $id = $model->id;
        }
        return $this->success(['id' => $id]);
    }


    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $id = \request("id");
        $info = ProducerAdv::find($id);
        if (!$info)
            return $this->failed('详情不存在');

        $merchant = $this->getUser();
        if ($merchant->category == Merchant::DAREN) {
            $exists = AdvSignUp::where('merchant_id', $merchant->id)
                ->where('adv_id', $id)->exists();
            $info->is_sign_up = $exists;
        }
        $m = Merchant::find($info->merchant_id);
        $info->fac_title = $m->fac_title ?? '';
        $info->sign_up_count = AdvSignUp::where('adv_id', $id)->count();
        return $this->success($info);
    }


    public function destroy(Request $request) {
        $this->validate($request, [
            'ids' => 'required'
        ]);
        \DB::transaction(function () use($request){
            $ids = $request->input('ids');
            $arr_ids = explode(",", $ids);
            foreach ($arr_ids as $id) {
                $info = ProducerAdv::find($id);
                if (!$info)
                    return $this->failed('详情不存在');
                $info->delete();
            }
        });
        return $this->success([]);
    }

}

